//
//  HMVisitorView.swift
//  微博
//
//  Created by 郭鉴增 on 2016/12/3.
//  Copyright © 2016年 郭鉴增. All rights reserved.
//

import UIKit
//定义代理协议
protocol HMVisitorViewDelegate: NSObjectProtocol {
    func loginOperation()
}

//访客视图控制器
class HMVisitorView: UIView {

    //定义需要执行的闭包
    var callBack: (() -> ())?
    //代理对象需要使用若引用,防止相互持有,产生吓唬矛盾
    weak var delegate: HMVisitorViewDelegate?
    
    //MARK: - 懒加载
    //旋转视图
    private lazy var cycleImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //主页视图
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.text = "关注一些人,回这里看看有什么惊喜  关注一些人,回这里看看有什么惊喜"
        label.textAlignment = .center
        //多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //注册
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //取消高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    //登录
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //取消高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    
    override init(frame : CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加控件 设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255 , alpha: 1)
        //添加控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //添加约束
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
      
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(222)
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        loginButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
    }
    //开启旋转动画
    private func startAnimation() {
        //创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //目的地
        animation.toValue = 2 * M_PI
        //动画执行时长
        animation.duration = 20
        //动画执行次数
        animation.repeatCount = MAXFLOAT
        //失去焦点不让其释放动画对象
        animation.isRemovedOnCompletion = false
        //执行动画
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    //修改访客视图
    func updateVisitorInfo(imageName: String?, message: String?) {
        
        if let imgName = imageName, let msg = message {
            //消息,发现,我的 可以进入,设置显示图片和信息,隐藏旋转视图            
            iconImageView.image = UIImage(named:  imgName)
            messageLabel.text = msg
            //隐藏旋转视图
            cycleImageView.isHidden = true
        }else{
            //只有首页进来,只有首页才开启动画
            startAnimation()
        }
    }
    //MARK: - 点击登录和注册按钮的逻辑处理
    @objc private func registerButtonAction() {
        print("注册")
        callBack?()
        //代理对象调动代理方法
        delegate?.loginOperation()
    }
    @objc private func loginButtonAction() {
        print("登录")
        callBack?()
        //代理对象调动代理方法 
        delegate?.loginOperation()
    }
    
    
    

}
